<?php
session_start();
// echo "sss".$_SESSION['user_name']."aaa".$_COOKIE['user_name'];
if (isset($_SESSION['user_name'])||isset($_COOKIE['user_name'])) {
  // 已登录，显示留言板
} else {
  // 未登录，跳转到登录页面
  header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="ASCII" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>留言板</title>
  <link rel="shortcut icon" href="./img/favicon.svg" type="image/x-icon">
  <link rel="stylesheet" href="./css/message_index.css">
</head>

<body>
  <div class="center">
    <div class="head">
      <div class="head_list user_name"><?php echo "欢迎，" . $_SESSION['user_pet']; ?> </div>
      <div class="head_list head_title">留言板</div>
      <div class="head_list logined_out" "><input type=" button" onclick="logout()" value="退出"></div>
    </div>
    <div class="message">
      <form class="message_from" action="message_db.php" method="post">
        <input type="text" name="message_title" placeholder="标题" />
        <textarea name="message_body" placeholder="写下你的留言！"></textarea>
        <input type="submit" />
      </form>
    </div>


    <?php
    setcookie("message-cookie", "wfbhiucafwbhiuwfbiu", time() + 3600);
    // 连接数据库
    require "./query_db.php";

    // 执行SQL查询，获取留言数据
    $sql = "SELECT * FROM message_bord";  //指定查询表message_bord表中的所有列
    $result = mysqli_query($conn, $sql);  //mysqli_query()用于执行SQL查询，并返回结果集或查询错误信息
    if (!$result) {  //$result接收错误信息或查询成功结果
      die("数据查询失败: " . mysqli_error($conn));
    }

    // mysqli_fetch_array()用于从结果集中取出一条记录并作为数组返回 MYSQL_NUM 用于以数字索引方式从结果集中取出数据，MYSQL_ASSOC 用于以关联数组方式从结果集中取出数据，默认是MYSQL_BOTH
    while ($message_data = mysqli_fetch_array($result, MYSQLI_NUM)) {
      echo "<div class='bord_list'>
        <div class='message_title'> $message_data[3] </div>
        <div class='message_body'>$message_data[4] </div>
        <div class='message_info'>
        <div class='message_user'>$message_data[2] </div>
        <div class='message_time'>$message_data[5] </div>
        </div>
        </div>";
    }
    ?>
    <script>
      function logout() {
        window.location.href = "logout.php";
      }
    </script>

  </div>
</body>

</html>